﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Hospital_TraBulidPayment : System.Web.UI.Page,HZ.MBSM.BLL.TraDes
{
   
    public Int32 BulidPayProject; 
       public Int64 BulidPayHospitalId;
    public int AccountId_PS;
   
    protected void Page_Load(object sender, EventArgs e)
    {

        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
           
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                BulidPayHospitalId = SysFun.ToLong(_Account.AccountId);
                BulidPayProject = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["ProjectId"]));
                AccountId_PS = SysFun.ToInt(SysFun.IsSQL(Request.Form["AccountId_PS"]));

            }
            if (!IsPostBack)
            {
                string _strHospitalName = _Account.DWName;
                txtPaymentName.Text = _Account.DWName + System.DateTime.Now.ToShortDateString() + "结算单";
            }
        }

    }
    protected void ButAdd_Click(object sender, EventArgs e)
    {
        if (Request.Form["txtCompanyName_PS"].ToString() == "")
        {
            Response.Write("<Script>alert('配送企业还没有选择')</script>");
        }
        else
        {
            if (Request.Form["Receiptid"].ToString() == "")
            {
                Response.Write("<Script>alert('发票号不能为空'')</script>");
            }

            else
            {
                if (this.txttotalMoney.Text.ToString() == "")
                {
                    Response.Write("<script>alert('总金额还没有查看')</script>");
                }
                else
                {
                    try
                    {
                        string receiptidstring = Request.Form["Receiptid"].ToString().Substring(0, Request.Form["Receiptid"].ToString().Length - 1);

                        string Paymentid = "";
                        string insertsql = "insert Payment(Accountid_YY,PaymentMemo,PaymentName,PaymentPrice,Accountid_PS,PaymentState) values('" + BulidPayHospitalId + "','" + this.txtPaymentMemo.Text.ToString() + "','" + txtPaymentName.Text + "','" + this.txttotalMoney.Text.ToString() + "','" + Request.Form["Accountid_PS"].ToString() + "','2')";
                        string selectQ = "select max(paymentid) as maxpaymentid from Payment";
                        Db db = new Db();
                        db.Command(insertsql);
                        System.Data.IDataReader dr = db.DataReader(selectQ);
                        while (dr.Read())
                        {
                            Paymentid = SysFun.ToTrim(dr["maxpaymentid"]);
                        }
                        dr.Close();
                        dr.Dispose();

                        string updatesql = "update BuyPlanGoods set ispayment=0,paymentid=" + Paymentid + " where Accountid_YY='" + BulidPayHospitalId + "' and Accountid_PS='" + Request.Form["Accountid_PS"].ToString() + "' and Receiptid in (" + receiptidstring + ")";

                        db.Command(updatesql);
                        db.Close();
                        Response.Write("<script>alert('保存成功')</script>");
                    }
                    catch( Exception ea)
                    {
                        Response.Write("<script>alert('发生错误，请重新建单！')</script>");
                    }
                    

                }
            }
        }
    

        //string _strSql, _strPaymentName, PPrice, ShouldPPrice, PaymentMemo, AccountId_PS, PaymentStartDate, PaymentEndDate;
        //_strPaymentName = this.txtPaymentName.Text;
        //PPrice = txtPaymentPrice.Text;
        //ShouldPPrice = txttotalMoney.Text.Trim();
        //PaymentMemo = txtPaymentMemo.Text;
        //AccountId_PS = Request.Form["AccountId_PS"];
        //PaymentStartDate = txtPaymentStartDate.Text;
        //PaymentEndDate = txtPaymentEndDate.Text;
        //_strPaymentName = SysFun.ToTrim(txtPaymentName.Text);
        ////_strSql = "select PaymentName from Payment where PaymentName='" + _strPaymentName + "' ";
        ////_strSql = "Select PaymentId From Payment Where AccountId_PS='" + AccountId_PS + "' And PaymentName='" + _strPaymentName + "'";
        ////Db db = new Db(); 
        ////System.Data.IDataReader dr;
        ////dr = db.DataReader(_strSql);
        //if (dr.Read())
        //{
        //    Response.Write("<script>window.alert('已经有此结算单名，请重新命名！');</script>");
        //    dr.Close();
        //    dr.Dispose();
        //    db.Close();
        //    dr = null;
        //    db = null;
        //    return;
        //}
        //dr.Close();
        //dr.Dispose();

        //_strSql = "Insert Into Payment(ProjectId,AccountId_YY,PaymentName,PaymentMemo,PaymentPrice,PaymentPriceShould,AccountId_PS,PaynamtDate,PaymentStartDate,PaymentEndDate) Values('" + BulidPayProject.ToString() + "','" + BulidPayHospitalId.ToString() + "','" + _strPaymentName + "','" + PaymentMemo + "','" + PPrice + "','" + ShouldPPrice + "','" + AccountId_PS + "','" + DateTime.Now + "','" + PaymentStartDate + "','" + PaymentEndDate + "') ";
        //db.Command(_strSql);
        //db.Close();
        //HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        //HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "插入", "Payment", _strSql, "../Hospital/TraBulidPayment.aspx");
        //Response.Write("<script>window.alert('保存成功！');</script>");
        //Response.Write("<script>window.location.href='Payment.aspx?ProjectId=" + BulidPayProject + "';</script>");

    }
    protected void btnClose_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.location.href='Payment.aspx?ProjectId=" + BulidPayProject + "';</script>");
    }
    /// <summary>
    /// 计算交易金额
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button2_Click(object sender, EventArgs e)
   {
       if (Request.Form["Receiptid"].ToString() == "")
       {
           Response.Write("<script>alert('发票号不能为空')</script>");
       }
       else
       {
             string receiptidstring = Request.Form["Receiptid"].ToString().Substring(0, Request.Form["Receiptid"].ToString().Length - 1);

           string allmomey = "";
           string Receiptid = SysFun.ToTrim(Request.Form["Receiptid"]);
           Db db = new Db();
           System.Data.IDataReader dr = db.DataReader("select sum(Tradeprice*InflowNum) as zong from BuyGoodsView_200708 where Receiptid in (" + receiptidstring + ") and Accountid_PS='" + Request.Form["Accountid_PS"].ToString() + "' and accountid_yy='" + BulidPayHospitalId + "' ");
           while (dr.Read())
           {
               allmomey = SysFun.ToTrim(dr["zong"]);
           }
           this.txttotalMoney.Text = allmomey.ToString();
           dr.Close();
           dr.Dispose();

           System.Data.IDataReader dt = db.DataReader("select Receiptid as 发票号,ProductName as 通用名,MedicineModel 剂型,Outlookc 规格,Pack 包装,Factor 转换系数,QualityLevels 质量层次,CompanyName_PS as 配送企业,Tradeprice as 价格,InflowNum as 数量,Tradeprice*InflowNum as 总金额 from BuyGoodsView_200708 where Receiptid in (" + receiptidstring + ") and accountid_yy ='" + BulidPayHospitalId + "'");
           this.GridView1.DataSource = dt;
           this.GridView1.DataBind();
           dt.Close();
           dt.Dispose();
           db.Close();
       }
    //    string _strAccountId_PS = SysFun.ToTrim(Request.Form["AccountId_PS"]);
    //    HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
    //    if (_Account != null && _strAccountId_PS != "")
    //    {
    //        String Sql, Money = "", _strWhere;
    //        _strWhere = "Where AccountId_PS='" + Request.Form["AccountId_PS"] + "' and DealStatus=2  and AccountId_YY=" + _Account.AccountId.ToString() + "    And ProjectId=" + BulidPayProject;
    //        if (txtPaymentStartDate.Text.Trim() != "")
    //        {
    //            _strWhere += " And hospitalinflowdate>='" + txtPaymentStartDate.Text.Trim() + "'";
    //        }
    //        if (txtPaymentEndDate.Text.Trim() != "")
    //        {
    //            _strWhere += " And hospitalinflowdate<='" + txtPaymentEndDate.Text.Trim() + "'";
    //        }

    //        Sql = "Select Convert(numeric(20,2),Sum(TradePrice*InflowNum)) as sum From BuyPlanGoods  " + _strWhere;
    //        Db db = new Db();
    //        System.Data.IDataReader dr;   
    //        dr = db.DataReader(Sql);
    //        while (dr.Read())
    //        {
    //            if (Convert.IsDBNull(dr["sum"]))
    //            {
    //                Money = "0";
    //            }
    //            else
    //            {
    //                Money = dr["sum"].ToString();
    //            }

    //        }
    //        txttotalMoney.Text = Money.ToString();

    //        dr.Close();
    ////        dr.Dispose();
    ////        db.Close();
    //    }
    }

    #region TraDes 成员

    public string Name
    {
        get { return "新增结算单"; }
    }

    public string Describe
    {
        get { return ""; }
    }

    public string ParentName
    {
        get { return "结算管理"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
            return _AccountType;
        }
    }

    #endregion
}
